package quq.missq.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import quq.missq.BaseActivity;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.adapter.SchoolAdapter;
import quq.missq.beans.School;
import quq.missq.utils.SchoolGeter;

/**
 * Created by River on 8/6/15.
 */
public class SchoolChooseActivity extends BaseActivity{

    private ListView mListView;
    private SchoolAdapter adapter;
    private ArrayList<School> schooles;
    private EditText mSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.school_choose_activity);
        initView();
        initListener();
    }

    private void initView(){
        mListView = (ListView)findViewById(R.id.listView);
        mSearch = (EditText)findViewById(R.id.search);
        new LoadSchool("").execute();
    }

    private void initListener(){
        mSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    String condition = mSearch.getText().toString();
                    if(!"".equals(condition)){
                        new LoadSchool(condition).execute();
                    }
                    return true;
                }
                return false;
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("school",adapter.getData().get(position));
                intent.putExtras(bundle);
                setResult(RESULT_OK,intent);
                SchoolChooseActivity.this.finish();
            }
        });
        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if("".equals(s.toString())){
                    new LoadSchool("").execute();
                }
            }
        });
    }

    private class LoadSchool extends AsyncTask<Void,Void,ArrayList<School>>{

        private String condition = "";

        public LoadSchool(String condition){
            this.condition = condition;
        }

        @Override
        protected ArrayList<School> doInBackground(Void... params) {
            SchoolGeter sg = new SchoolGeter(SchoolChooseActivity.this);
            MLog.e("***",sg.getSchool().size()+"");

            if("".equals(condition)){
                return sg.getSchool();
            }else{
                return sg.getSchool(condition);
            }
        }

        @Override
        protected void onPostExecute(ArrayList<School> schools) {
            schooles = new ArrayList<>();
            adapter = new SchoolAdapter(SchoolChooseActivity.this);
            adapter.setData(schools);
            mListView.setAdapter(adapter);
        }
    }

}
